package com.example.administrator.caigou51.activity;

import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.ab.util.AbViewUtil;
import com.alibaba.fastjson.JSON;
import com.example.administrator.caigou51.EventBusClass.UpdateGoodsDetailViewPager;
import com.example.administrator.caigou51.EventBusClass.UpdateZhuanQuByCatID;
import com.example.administrator.caigou51.R;
import com.example.administrator.caigou51.bean.ZhuangQuXiangQingBean;
import com.example.administrator.caigou51.global.App;
import com.example.administrator.caigou51.global.Constant;
import com.example.administrator.caigou51.recyclerCard.basic.MaterialListView;
import com.example.administrator.caigou51.recyclerCard.basic.SuperDIYSwipeRefreshLayout;
import com.example.administrator.caigou51.recyclerCard.card.UseSayDetailCard;
import com.example.administrator.caigou51.recyclerCard.card.ZhuangQu1ChildCard;
import com.example.administrator.caigou51.recyclerCard.card.ZhuangQuCard1;
import com.example.administrator.caigou51.recyclerCard.card.ZhuangQuCard2;
import com.example.administrator.caigou51.request.BaseResponse;
import com.example.administrator.caigou51.request.ICallBackForRequestServer;
import com.example.administrator.caigou51.request.RequestServerManager;

import de.greenrobot.event.EventBus;

/**
 * Created by Administrator on 2016/8/18/018.
 */
public class ZhuangQuActivity extends GBaseActivity{

    MaterialListView listview;
    private String title;
    public static final String TitleTag="TitleTag";
    public static  final String CatIDTag="CatIDTag";
    String catid;
    ZhuangQuXiangQingBean zhuangQuXiangQingBean;
    MaterialListView materialListViewTitle;
    private ImageView imageViewMore;
    private TextView textViewSYFL;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentViewFor720_1280(R.layout.activity_zhuanqu);
        findViewsById();
        initView();
        taskGetZhuangQuXiangQing(false);
    }

    private void initView() {
        EventBus.getDefault().register(this);
        App.getInstance().setScrollListViewHideBottom(listview,this);

        title=getIntent().getStringExtra(TitleTag);
        catid=getIntent().getStringExtra(CatIDTag);
        setTopTitle(title);
        setRightImg(View.VISIBLE,R.drawable.icon_more_net_white);


        materialListViewTitle= (MaterialListView) findViewById(R.id.materialListViewTitle);
        materialListViewTitle.setVisibility(View.VISIBLE);
        imageViewMore= (ImageView) findViewById(R.id.imageViewMore);
        textViewSYFL= (TextView) findViewById(R.id.textViewSYFL);textViewSYFL.setVisibility(View.GONE);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
        materialListViewTitle.setLayoutManager(linearLayoutManager);
   //     materialListViewTitle.getLayoutParams().height= AbViewUtil.scaleValue(this,86);

        imageViewMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopupWindow(imageViewMore);
                imageViewMore.setImageResource(R.drawable.icon_shangbiao);
                textViewSYFL.setVisibility(View.VISIBLE);
                materialListViewTitle.setVisibility(View.GONE);
            }
        });
    }

    @Override
    public void doClickRightImg() {
        App.showPopupWindow(getRightImageView(),ZhuangQuActivity.this,-124,-30);
    }

    private void findViewsById() {


        textViewSYFL= (TextView) findViewById(R.id.textViewSYFL);
        imageViewMore= (ImageView) findViewById(R.id.imageViewMore);
        listview= (MaterialListView) findViewById(R.id.listview);
        materialListViewTitle= (MaterialListView) findViewById(R.id.materialListViewTitle);

    }
    private void fillArrayInListView(ZhuangQuXiangQingBean zhuangQuXiangQingBean) {

        //没有子分区就不显示
      /*  if (zhuangQuXiangQingBean.getSub_cat()!=null && zhuangQuXiangQingBean.getSub_cat().size()>0) {


            ZhuangQuCard1 zhuangQuCard1 = new ZhuangQuCard1(this);
            zhuangQuCard1.setIndex(0);
            zhuangQuCard1.setZhuangQuXiangQingBean(zhuangQuXiangQingBean);
            listview.add(zhuangQuCard1);
        }*/
        for (int i=0;i<zhuangQuXiangQingBean.getSub_cat().size();i++)
        {
            ZhuangQu1ChildCard zhuangQu1ChildCard = new ZhuangQu1ChildCard(this);
            zhuangQu1ChildCard.setPosition(i);
            zhuangQu1ChildCard.setMaterialListView(materialListViewTitle);
            zhuangQu1ChildCard.setSubCatEntity(zhuangQuXiangQingBean.getSub_cat().get(i));
            if (i==0)
                zhuangQu1ChildCard.setIschecked(true);
            materialListViewTitle.add(zhuangQu1ChildCard);
        }

        for (int i=0;i<zhuangQuXiangQingBean.getGoods().size();i++)
        {
            ZhuangQuCard2 zhuangQuCard2=new ZhuangQuCard2(this);
            zhuangQuCard2.setShowBottomLine(true);
            zhuangQuCard2.setGoodsBean(zhuangQuXiangQingBean.getGoods().get(i));
            listview.add(zhuangQuCard2);
        }
    }

    private void taskGetZhuangQuXiangQing(boolean isloadmore)
    {

        RequestServerManager.getInstanceDIY().handleMethodDIY(this, null, isloadmore, null,
                Constant.getRootUrl() + Constant.Action.Action_ZhuanQuXiangQing, Constant.Action.TokenAct_MainData, RequestServerManager.getInstanceDIY().
                        getRequestParams(Constant.Action.Action_ZhuanQuXiangQing,"200", "1",catid), BaseResponse.class, new ICallBackForRequestServer<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse result) {
                        // JSONObject jsonObject = (JSONObject) result.getData();
                        zhuangQuXiangQingBean = JSON.parseObject(result.getData().toString(), ZhuangQuXiangQingBean.class);
                        fillArrayInListView(zhuangQuXiangQingBean);

                    }

                    @Override
                    public void onFail(int type, String errorMsg, String json) {

                    }
                });

    }
    private void taskGetZhuangQuXiangQingByID(boolean isloadmore, final int position)
    {

        RequestServerManager.getInstanceDIY().handleMethodDIY(this, null, isloadmore, null,
                Constant.getRootUrl() + Constant.Action.Action_ZhuanQuXiangQing, Constant.Action.TokenAct_MainData, RequestServerManager.getInstanceDIY().
                        getRequestParams(Constant.Action.Action_ZhuanQuXiangQing,"200", "1",catid), BaseResponse.class, new ICallBackForRequestServer<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse result) {
                        // JSONObject jsonObject = (JSONObject) result.getData();
                       // for (int i=1;i<listview.getAdapter().getItemCount();i++)
                      //  {
                            listview.clear();
                      //  }
   /*                     ZhuangQuCard1 zhuangQuCard1 = new ZhuangQuCard1(ZhuangQuActivity.this);
                        zhuangQuCard1.setIndex(position);
                        zhuangQuCard1.setZhuangQuXiangQingBean(zhuangQuXiangQingBean);
                        listview.add(zhuangQuCard1);*/

                        materialListViewTitle.clear();
                        for (int i=0;i<zhuangQuXiangQingBean.getSub_cat().size();i++)
                        {
                            ZhuangQu1ChildCard zhuangQu1ChildCard = new ZhuangQu1ChildCard(ZhuangQuActivity.this);
                            zhuangQu1ChildCard.setPosition(i);
                            zhuangQu1ChildCard.setMaterialListView(materialListViewTitle);
                            zhuangQu1ChildCard.setSubCatEntity(zhuangQuXiangQingBean.getSub_cat().get(i));
                            if (i==position)
                                zhuangQu1ChildCard.setIschecked(true);
                            materialListViewTitle.add(zhuangQu1ChildCard);
                        }

                        ZhuangQuXiangQingBean zhuangQuXiangQingBean2 = JSON.parseObject(result.getData().toString(), ZhuangQuXiangQingBean.class);
                        for (int i=0;i<zhuangQuXiangQingBean.getGoods().size();i++)
                        {
                            ZhuangQuCard2 zhuangQuCard2=new ZhuangQuCard2(ZhuangQuActivity.this);
                            zhuangQuCard2.setGoodsBean(zhuangQuXiangQingBean2.getGoods().get(i));
                            listview.add(zhuangQuCard2);
                        }

                    }

                    @Override
                    public void onFail(int type, String errorMsg, String json) {

                    }
                });

    }
    public void onEventMainThread(UpdateZhuanQuByCatID updateZhuanQuByCatID) {
        catid=updateZhuanQuByCatID.catID;
        taskGetZhuangQuXiangQingByID(false,updateZhuanQuByCatID.position);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
    private void showPopupWindow(View view) {

        // 一个自定义的布局，作为显示的内容
        View contentView = LayoutInflater.from(this).inflate(
                R.layout.layout_more_fenlei, null);

        AbViewUtil.scaleContentView((ViewGroup) contentView);

        final PopupWindow popupWindow = new PopupWindow(contentView,
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, true);

        //  AbViewUtil.scaleContentView((ViewGroup) popupWindow.getContentView());

        MaterialListView listView= (MaterialListView) contentView.findViewById(R.id.material_listview);
        listView.setColumnLayout(3);

        for (int i=0;i<zhuangQuXiangQingBean.getSub_cat().size();i++)
        {
            UseSayDetailCard useSayDetailCard=new UseSayDetailCard(this);
            useSayDetailCard.setSubCatEntity(zhuangQuXiangQingBean.getSub_cat().get(i));
            useSayDetailCard.setPopupWindow(popupWindow);
            useSayDetailCard.setImageViewMore(imageViewMore);
            useSayDetailCard.setTextViewSYFL(textViewSYFL);
            useSayDetailCard.setMaterialListView(materialListViewTitle);
            useSayDetailCard.setIndex(i);
            useSayDetailCard.setCatid(catid);
            listView.add(useSayDetailCard);
        /*    useSayDetailCard.setOnNormalButtonPressedListener(new OnButtonPressListener() {
                @Override
                public void onButtonPressedListener(View view, Card card) {
                    Log.e("materialListView", "onTouch : ");

                    popupWindow.dismiss();
                    imageViewMore.setImageResource(R.drawable.icon_xiabiao);
                    textViewSYFL.setVisibility(View.GONE);
                    materialListView.setVisibility(View.VISIBLE);
                }
            });*/
        }

/*        View viewBg=contentView.findViewById(R.id.viewBg);
        viewBg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });*/
        //   popupWindow.setTouchable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setFocusable(true);

      /*  popupWindow.setTouchInterceptor(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                Log.e("mengdd", "onTouch : ");
                popupWindow.dismiss();
                return false;
                // 这里如果返回true的话，touch事件将被拦截
                // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss
            }
        });*/

        // 如果不设置PopupWindow的背景，无论是点击外部区域还是Back键都无法dismiss弹框
        // 我觉得这里是API的一个bug
        //  popupWindow.setBackgroundDrawable(getResources().getDrawable(new BitmapDrawable());
        popupWindow.setBackgroundDrawable(new BitmapDrawable());

        // 设置好参数之后再show
        popupWindow.showAsDropDown(view,0,AbViewUtil.scaleValue(this,4));
        // popupWindow.showAtLocation(view, Gravity.RIGHT|Gravity.BOTTOM, 0, 0);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                popupWindow.dismiss();
                imageViewMore.setImageResource(R.drawable.icon_xiabiao);
                textViewSYFL.setVisibility(View.GONE);
                materialListViewTitle.setVisibility(View.VISIBLE);
            }
        });
    }
}
